package com.temperate;

import com.test.FileUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @author:wesley
 */
public class RunJob {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {


        //设置hadoop环境变量
        System.setProperty("hadoop.home.dir","E:\\devTools\\hadoop-2.7.6\\hadoop-2.7.6");

        //删除out文件目录
        FileUtil.deleteDir("output");


        Configuration conf = new Configuration();

        String[] otherArgs = new String[]{"input/weather.txt", "output/wc"};
        if (otherArgs.length != 2) {
            System.err.println("Usage:Merge and duplicate removal <in> <out>");
            System.exit(2);
        }

        //job名
        Job job = Job.getInstance(conf, "temperateJob");
        job.setJarByClass(RunJob.class);


        job.setMapperClass(TempMapper.class);
        job.setReducerClass(TempReduce.class);
        job.setGroupingComparatorClass(TempGroup.class);
        job.setSortComparatorClass(TempSort.class);
        job.setPartitionerClass(TempPartition.class);

        job.setOutputKeyClass(TempKey.class);
        job.setOutputValueClass(DoubleWritable.class);

        job.setNumReduceTasks(3);
        job.setInputFormatClass(KeyValueTextInputFormat.class);

        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }



}
